<template>
  <div>
    <Table
      ref="tableRef"
      :columns="data.columns"
      :handles="data.handles"
      :toolbar="data.toolbar"
      :query="data.query"
      @get-list="getData"
    ></Table>
    <DialogEdit ref="dialogEditRef" @get-data="getTable"></DialogEdit>
    <DialogVoided ref="dialogVoidedRef" @get-data="getTable"></DialogVoided>
    <DialogCosting ref="dialogCostingRef"></DialogCosting>
  </div>
</template>

<script lang="ts" setup name="couponList">
import Table from '@/components/table/index.vue';
import DialogEdit from './dialogEdit.vue';
import DialogVoided from './dialogVoided.vue';
import DialogCosting from './dialogCosting.vue';
import { pagingActivity } from '@/service/apis/market';
import { queryDicByParent } from '@/service/apis/api';
import { queryBusAndStation } from '@/service/apis/org';

const router = useRouter();

const tableRef = ref();
const dialogEditRef = ref();
const dialogVoidedRef = ref();
const dialogCostingRef = ref();

const emit = defineEmits(['get-update']);
onActivated(() => {
  emit('get-update', (flag: boolean) => {
    if (flag) {
      tableRef.value?.getData();
    }
  });
});

queryDicByParent({
  listDtos: ['couponType', 'couponDiscountType', 'couponActivityStatus'],
}).then((res: any) => {
  if (res) {
    data.columns[1].dic = [...res.couponType];
    data.columns[2].dic = [...res.couponDiscountType];
    data.columns[10].dic = [...res.couponActivityStatus];
    data.query[2].props.options = [...res.couponType];
    data.query[3].props.options = [...res.couponDiscountType];
    data.query[4].props.options = [...res.couponActivityStatus];
  }
});

// 获取承担放
queryBusAndStation({}).then((res: any) => {
  if (res) {
    data.query[1].props.options = [...res.listMap];
  }
});

const data = reactive({
  columns: [
    {
      prop: 'name',
      label: '优惠券名称',
      width: 100,
    },
    {
      prop: 'couponType',
      label: '优惠券种类',
      dic: [] as any,
      type: Number,
      width: 90,
    },
    {
      prop: 'discountType',
      label: '抵扣费用类型',
      dic: [] as any,
      type: Number,
      width: 110,
    },
    {
      prop: 'discountAmount',
      label: '面额/折扣',
      width: 90,
      template: (row: any) => {
        return row.couponType === 1
          ? `${row.discountAmount}元`
          : `${row.discountRatio}%`;
      },
    },
    {
      prop: 'couponCount',
      label: '总数量',
      dataType: String,
    },
    {
      prop: 'receivedCount',
      label: '已领数量',
      dataType: String,
    },
    {
      prop: 'writeOffCount',
      label: '已核销数量',
      width: 90,
      dataType: String,
    },
    {
      prop: 'introduce',
      label: '使用说明',
      width: 120,
    },
    {
      prop: 'updateBy',
      label: '修改人',
    },
    {
      prop: 'updateTime',
      label: '修改时间',
      width: 150,
    },
    {
      prop: 'status',
      label: '优惠券状态',
      dic: [] as any,
      width: 90,
      type: Number,
    },
    {
      prop: 'reason',
      label: '作废原因',
      width: 150,
    },
  ],
  handles: [
    {
      label: '优惠券详情',
      handle: (row: any) => {
        console.log('详情 ---', row);
        router.push({
          name: 'couponDetail',
          query: { id: row.id },
        });
      },
    },
    {
      label: '发放明细 ',
      permission: 'couponList.grantDetails',
      handle: (row: any) => {
        console.log('详情 ---', row);
        router.push({
          name: 'couponTaskDetail',
          query: { id: row.id },
        });
      },
    },
    {
      label: '修改',
      permission: 'couponList.edit',
      disabledFun: (row: any) => {
        return row.status !== 0;
      },
      handle: (row: any) => {
        console.log('编辑---', row);
        dialogEditRef.value?.show(row);
      },
    },
    {
      label: '作废',
      color: '#FA3939',
      permission: 'couponList.cancel',
      disabledFun: (row: any) => {
        return row.status !== 0;
      },
      handle: (row: any) => {
        dialogVoidedRef.value?.show(row);
      },
    },
    {
      label: '复制',
      permission: 'couponList.copy',
      handle: (row: any) => {
        console.log('复制---', row);
        router.push({
          name: 'couponAdd',
          query: { id: row.id },
        });
      },
    },
    {
      label: '成本核算',
      permission: 'couponList.cost',
      handle: (row: any) => {
        console.log('成本核算---', row);
        dialogCostingRef.value?.show(row);
      },
    },
  ],
  toolbar: [
    {
      label: '新增',
      permission: 'couponList.add',
      icon: 'icon-add',
      type: 'primary',
      handle: (row: any) => {
        router.push({
          name: 'couponAdd',
        });
      },
    },
    {
      label: '发放',
      permission: 'couponList.grant',
      type: 'primary',
      handle: (row: any) => {
        router.push({
          name: 'couponTask',
        });
      },
    },
  ],
  query: [
    {
      label: '优惠券名称',
      prop: 'name',
      comName: 'input',
      props: {
        placeholder: '请输入优惠券名称',
      },
    },
    {
      label: '承担方',
      prop: 'orgId',
      comName: 'select',
      props: {
        placeholder: '请选择承担方',
        change: (value: any) => {
          console.log(value);
        },
        options: [] as any,
      },
    },
    {
      label: '优惠券种类',
      prop: 'couponType',
      comName: 'select',
      props: {
        placeholder: '请选择优惠券种类',
        change: (value: any) => {
          console.log(value);
        },
        options: [] as any,
      },
    },
    {
      label: '抵扣费用类型',
      prop: 'discountType',
      comName: 'select',
      props: {
        placeholder: '请选择抵扣费用类型',
        change: (value: any) => {
          console.log(value);
        },
        options: [] as any,
      },
    },
    {
      label: '状态',
      prop: 'status',
      comName: 'select',
      props: {
        placeholder: '请选择状态',
        change: (value: any) => {
          console.log(value);
        },
        options: [] as any,
      },
    },
  ],
});

const getData = (page: any, search: any) => {
  const form = { ...search.ruleForm };
  if (form.orgId) {
    form.dutyList = [{ orgId: form.orgId }];
  }
  delete form.orgId;
  pagingActivity(form, page).then((res: any) => {
    if (res) {
      if (page.pageNo !== 1 && (!res.dataList || res.dataList?.length === 0)) {
        page.pageNo--;
        getData(page, search);
        return;
      }
      nextTick(() => {
        tableRef.value?.setTable({
          total: res.totalCount,
          table: res.dataList,
        });
      });
    }
  });
};

const getTable = () => {
  tableRef.value?.getData();
};
</script>